# ---
# kind: StorageClass
# apiVersion: storage.k8s.io/v1
# metadata:
#   name: local-storage
# provisioner: kubernetes.io/no-provisioner
# volumeBindingMode: WaitForFirstConsumer

---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: nginx-local-pv
  labels:
    type: local
spec:
  storageClassName: local-storage
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/home/nginx"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-local-pvc
  labels:
    type: local
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
data:
  default.conf: |
      server {
            listen 80 default_server;
            listen [::]:80 default_server;
            root /var/www/html;
            server_name  _;
            index  index.php index.html index.htm;
            location / {
                try_files $uri $uri/ =404;
            }
            location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass phpfpm:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param   PATH_INFO       $fastcgi_path_info;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
            }
        }
    
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx      
    spec:
      containers:
      - name: nginx
        image: nginx
        resources:
          limits:
            memory: "512Mi"
            cpu: "500m"
          requests:
            memory: "256Mi"
            cpu: "200m"
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-volume
          mountPath: /etc/nginx/conf.d 
        - mountPath: /var/www/html/
          name: nginx-data
      volumes:
      - name: nginx-volume
        configMap:
          name: nginx-conf
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nginx-local-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30071
  selector:
    app: nginx
    
---
kind: Service
apiVersion: v1
metadata:
  name: phpfpm
  labels:
    app: phpfpm
    layer: backend
spec:
  type: ClusterIP
  selector:
    app: phpfpm
  ports:
    - port: 9000
      targetPort: 9000

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: phpfpm
  labels:
    app: phpfpm
    layer: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: phpfpm
  template:
    metadata:
      labels:
        app: phpfpm
    spec:
      containers:
        - name: phpfpm
          image: bitnami/php-fpm
          resources:
            limits:
              memory: "512Mi"
              cpu: "500m"
            requests:
              memory: "256Mi"
              cpu: "200m"
          ports:
            - containerPort: 9000
          volumeMounts:
            - mountPath: /var/www/html/
              name: nginx-data
      volumes:
        - name: nginx-data
          persistentVolumeClaim:
            claimName: nginx-local-pvc



